// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2025-02-12

package customer

import (
    "context"
    "fmt"
    "integration/app/lib/conf"
    "integration/app/models"
)

// Entity
// 上报客户入参.
//
//   {
//      "id":83,
//      "name":"晨太客户-老付",
//      "number":"KH0735",
//      "assort":"",
//      "grade":"",
//      "frame":113,
//      "user":"",
//      "supplier":"",
//      "person":17,
//      "file":[],
//      "title":"",
//      "tax":"",
//      "bank":"",
//      "account":"",
//      "type":0,
//      "field":{"a":"","b":"","c":"","d":"","e":""},
//      "state":true,
//      "data":"",
//      "contact":[
//          {
//              "add":"江南省",
//              "tel":"13966013721",
//              "data":"收货地址",
//              "name":"老付"
//          }
//      ],
//      "py":"ctkh*lf",
//      "balance":0
//   }
type Entity struct {
    Account  string     `json:"account"`
    Assort   int        `json:"assort" label:"客户类别"`
    Balance  float64    `json:"balance"`
    Bank     string     `json:"bank"`
    Contact  []*Contact `json:"contact" label:"联系资料"`
    Data     string     `json:"data" label:"备注"`
    Field    *Field     `json:"field" label:"扩展资料"`
    File     []any      `json:"file" label:"客户附件"`
    Frame    int        `json:"frame" label:"组织结构"`
    Id       int        `json:"id" label:"客户ID"`
    Name     string     `json:"name" label:"客户名称"`
    Number   string     `json:"number" label:"客户编号"`
    Person   int        `json:"person" label:"关联员工"`
    Py       string     `json:"py" label:"拼音"`
    State    bool       `json:"state" label:"启用状态"`
    Supplier string     `json:"supplier" label:"关联供应商"`
    Tax      string     `json:"tax" label:"开票税号"`
    Title    string     `json:"title" label:"开票抬头"`
    Type     int        `json:"type" label:"客户类型"`
    User     int        `json:"user" label:"隶属用户"`
}

func New() *Entity {
    return &Entity{
        Contact: make([]*Contact, 0),
        Field:   NewField(),
        File:    make([]any, 0),
    }
}

func (o *Entity) NotChanged(bean *models.ErpCustomer) bool {
    for _, v := range [][]any{
        {bean.ErpFrame, o.Frame},
        {bean.ErpName, o.Name},
        {bean.ErpNumber, o.Number},
        {bean.ErpContactName, o.Contact[0].Name},
        {bean.ErpContactAddress, o.Contact[0].Address},
        {bean.ErpContactPhone, o.Contact[0].Phone},
    } {
        if v[0] != v[1] {
            return false
        }
    }
    return true
}

// Parse
// 解析订单数据.
func (o *Entity) Parse(ctx context.Context, agent *conf.ErpAgent, order *models.Order) (err error) {
    o.Assort = agent.CustomerType
    o.Data = fmt.Sprintf(`%s`, order.OrderId)
    o.Frame = agent.OrganizationId
    o.Name = agent.GenerateCustomerName(order.Uid, order.RealName)
    o.Number = agent.GenerateCustomerNo(order.Uid)
    o.Person = agent.ManagerId
    o.State = true
    o.User = agent.CreatorId

    // 联系资料.
    err = o.ParseContact(ctx, order)
    return
}

// ParseContact
// 解析联系方式.
func (o *Entity) ParseContact(_ context.Context, order *models.Order) (err error) {
    contact := NewContact()
    contact.Name = order.RealName
    contact.Address = order.UserAddress
    contact.Phone = order.UserPhone
    o.Contact = append(o.Contact, contact)
    return
}
